﻿using Main.DataBaseContext;
using Main.dto;
using Main.enums;
using Main.messageEvent;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using static Main.App;

namespace Main.ViewModels
{
    public class IndexViewModel : BindableBase
    {
        private readonly BusnessContext busnessContext;
        private readonly IDialogService dialogService;
        private readonly IEventAggregator eventAggregator;

        public ICommand AddToDoTask { get; set; }

        public ICommand ShowAddMemoDialog { get; set; }

        public ICommand ShowMenuBar { get; set; }


        public ToDoTask SelectedToDoTask { get; set; }

        public ObservableCollection<TaskBar> TaskBars { get; set; }

        public IndexViewModel(BusnessContext busnessContext,IDialogService dialogService,IEventAggregator eventAggregator)
        {
            this.busnessContext = busnessContext;
            this.dialogService = dialogService;
            this.eventAggregator = eventAggregator;

            AddToDoTask = new DelegateCommand<string>(ShowAddCommandTaskDialog);
            ShowAddMemoDialog = new DelegateCommand<string>(ShowAddMemoDialogHandler);

            TaskBars = new ObservableCollection<TaskBar> { 
            
                new TaskBar("摘要","#006ec6","Alarm"),
                new TaskBar("待办","#3a7e00","FileDocumentArrowRightOutline")
            };
        }

        private void ShowAddMemoDialogHandler(string arg)
        {
            if (SelectedToDoTask is null)
            {

                MessageBox.Show("必须先选中一个待办任务才能添加备忘录");
                return;
            }
            DialogParameters dialogParameters = new DialogParameters();

            dialogParameters.Add("toDoTask", SelectedToDoTask);

            dialogService.ShowDialog(AddMemoDialogName, dialogParameters, DialogResultHandler);
        }


        private void ShowAddCommandTaskDialog(string args)
        {

            dialogService.ShowDialog(AddToDoTaskDialogName, DialogResultHandler);
        }



        private void DialogResultHandler(IDialogResult result)
        {
            if (result.Result == ButtonResult.OK)
            {
                eventAggregator.GetEvent<RefreshMainWindowEvent>().Publish("hello");
            }

        }

       
    }
}
